package org.zjx.action.route;

import org.zjx.action.basic.DelayAction;
import org.zjx.action.basic.ScreenshotAction;
import org.zjx.action.game.AbstractWindowAction;
import org.zjx.action.game.GameAction;
import org.zjx.analysis.charm.FlyingCharmAnalysis;
import org.zjx.analysis.window.InventorAnalysis;
import org.zjx.core.IWindow;
import org.zjx.entity.Address;
import org.zjx.enums.prop.SlotType;
import org.zjx.model.prop.ItemInstance;
import org.zjx.state.ItemState;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

//使用飞行符
public class FlyingCharmAction extends AbstractWindowAction {
    private InventorAnalysis inventorAnalysis;
    private FlyingCharmAnalysis flyingCharmAnalysis;
    private final String cityName;

    public FlyingCharmAction(IWindow targetWindow, String cityName) {
        super(targetWindow, false);
        this.cityName = cityName;
    }

    @Override
    protected void doExecute() throws Exception {
        if (!getAddressNow().near(FlyingCharmAnalysis.targetAddress(cityName),5)){
            useFlyingCharm();
        }
    }

    private void useFlyingCharm() {
        open();
        reSetWindow();
        int index = getInventorArea();
        if (index == -1) {
            //TODO 需要购买飞行符了
            throw new IllegalArgumentException("缺少飞行符！！！");
        } else {
            Rectangle flyingArea = inventorAnalysis.getItemIndexArea(index);
            do {
                clickRightMouse(flyingArea);
                new DelayAction(targetWindow,500).execute();
                new ScreenshotAction(targetWindow).execute();
                analysisFlyingCharm();
            } while (!flyingCharmAnalysis.isOpened());

            Rectangle rectangle = flyingCharmAnalysis.getCityLocation(cityName);

            do {
                click(rectangle.getCenterX(), rectangle.getCenterY());
                new DelayAction(targetWindow,1500).execute();
                new ScreenshotAction(targetWindow).execute();
                analysisFlyingCharm();
            } while (flyingCharmAnalysis.isOpened());
        }

        close();
    }

    private void clickRightMouse(Rectangle rectangle) {
        targetWindow.execute(op -> {
            op.gameMove(rectangle);
            op.delay(50);
            op.click(2);
            op.delay(50);
            return null;
        });
    }

    //获取飞行符所在位置
    private int getInventorArea() {
        List<ItemInstance> itemInstances = stateManager.getState(ItemState.class).getItemsByType(SlotType.INVENTORY);
        for (int i = 0; i < itemInstances.size(); i++) {
            ItemInstance itemInstance = itemInstances.get(i);
            if (itemInstance == null) continue;
            if (itemInstance.getName().equals("飞行符")) return i;
        }
        return -1;
    }


    private void analysis() {
        inventorAnalysis = new InventorAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
    }

    private void analysisFlyingCharm() {
        flyingCharmAnalysis = new FlyingCharmAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
    }

    @Override
    public void open() {
        analysis();
        mouseAloofWindow(inventorAnalysis.windowArea == null ? inventorAnalysis.getOriginBounds() : inventorAnalysis.windowArea);
        if (inventorAnalysis.isOpened()) return;
        hotkey(KeyEvent.VK_ALT, KeyEvent.VK_E);
        flush();
        open();
    }

    @Override
    public void close() {
        analysis();
        if (!inventorAnalysis.isOpened()) return;
        hotkey(KeyEvent.VK_ALT, KeyEvent.VK_E);
        flush();
        close();
    }

    @Override
    public void reSetWindow() {
        if (inventorAnalysis.isTransborder()) {
            drag(inventorAnalysis.conversion(inventorAnalysis.windowArea, inventorAnalysis.getRefImageArea()), inventorAnalysis.conversion(inventorAnalysis.getOriginBounds(), inventorAnalysis.getRefImageArea()));
            analysis();
        }
    }

    @Override
    public void mouseAloofWindow(Rectangle rect) {
        if (inventorAnalysis.mouseInRect(rect)) {
            Point safePoint = inventorAnalysis.findLargestRemainingAreaCenter(rect);
            move(safePoint);
        }
    }

    public Address getAddressNow() {
        new ScreenshotAction(targetWindow).execute();
        new GameAction(targetWindow).execute();
        return targetWindow.getState().getGameState().getAddress();
    }
}
